//
//  MBProgressHUDViewController.m
//  GRJTolCayFrwk
//
//  Created by 易上云 on 2017/3/9.
//  Copyright © 2017年 Global Barter. All rights reserved.
//

#import "MBProgressHUDViewController.h"
#import "MBProgressHUD.h"

@interface MBProgressHUDViewController ()<MBProgressHUDDelegate>

@property(nonatomic,strong)MBProgressHUD *hud;

@end

@implementation MBProgressHUDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"MBProgressHUD";
    [self setUpHUDButtons];
    
}

-(void)setUpHUDButtons{
    
    NSArray *array = @[@"样式一",@"样式二",@"样式三",@"样式四",@"样式五",@"样式六"];
    
    for (int i=0; i<array.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * (self.view.bounds.size.width/array.count), 100, self.view.bounds.size.width/array.count, 44);
        button.tag = i+99;
        
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor grayColor]];
        [button addTarget:self action:@selector(hUDButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:button];
    }
    
    
    
}

-(void)hUDButtonClick:(UIButton *)sender{
    
    
    NSLog(@"%ld",sender.tag);
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud = hud;
    
    hud.delegate = self;
    
    hud.bezelView.color = [UIColor greenColor];
//    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
    
    hud.backgroundView.color = [UIColor orangeColor];
    hud.backgroundView.alpha = 0.2;
//    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    //标题文字
    hud.label.text = @"标题文字";
    //详情文字
    hud.detailsLabel.text = @"详情文字详情文字详情文字详情文字详情文字详情文字详情文字详情文字详情文字详情文字详情文字详情文字详情文字";
    //内容颜色
    hud.contentColor = [UIColor redColor];
    // 背景框的透明度，默认值是0.8
    hud.completionBlock = ^{
        NSLog(@"completionBlock");
    };
    
    if (sender.tag == 99) {
        //1 使用UIActivityIndicatorView来显示进度，这是默认值
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.animationType = MBProgressHUDAnimationFade;
    }else if (sender.tag == 100) {
        //2 使用一个圆形饼图来作为进度视图
        hud.mode = MBProgressHUDModeDeterminate;
        hud.animationType = MBProgressHUDAnimationZoom;
        
        [self progress];
    }else if(sender.tag == 101){
        //3 使用一个水平进度条
        hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
        hud.animationType = MBProgressHUDAnimationZoomOut;
        
        [self progress];
    }else if(sender.tag == 102){
        //4 使用圆环作为进度条
        hud.mode = MBProgressHUDModeAnnularDeterminate;
        hud.animationType = MBProgressHUDAnimationZoomIn;
        
        [self progress];
    }else if(sender.tag == 103){
        //5 显示一个自定义视图，通过这种方式，可以显示一个正确或错误的提示图
        hud.mode = MBProgressHUDModeCustomView;
        
        UIImage *image = [UIImage imageNamed:@"QQ"];
        UIImageView *imgV = [[UIImageView alloc] initWithImage:image];
        [imgV.layer addAnimation:[self makeRotation] forKey:nil];
        
        hud.customView = imgV;
        //hud.square = YES;
    }else if(sender.tag == 104){
        //6 只显示文本
        hud.mode = MBProgressHUDModeText;
    }
    
}

-(void)progress{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (float i = 0.001;i<1 ; i += 0.001) {
            //NSLog(@"%@",[NSThread currentThread]);
            NSLog(@"wqdqwd");
            dispatch_async(dispatch_get_main_queue(), ^{
                self.hud.progress =i;
                //NSLog(@"---%@",[NSThread currentThread]);
            });
        }
    });
}

-(CABasicAnimation *)makeRotation{
    CATransform3D rotationTransform = CATransform3DMakeRotation((360*180.0)/(M_PI), 0, 0, -1);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    
    animation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
    
    animation.duration  =  1;
    
    animation.autoreverses = NO;
    
    animation.cumulative = YES;
    
    animation.fillMode = kCAFillModeForwards;
    
    animation.repeatCount = 100;
    
    return animation;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [_hud hideAnimated:YES afterDelay:0.0];
//    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

-(void)hudWasHidden:(MBProgressHUD *)hud{
    NSLog(@"%s",__func__);
}

@end
